const express = require('express')
// 导入数据库模块
const db = require('../db')

let Token = require('../utils/jwtutil')

// 创建路由对象
const router = express.Router()

// 登录接口
router.get('/login', (req, res) => {
    let account = req.query.account
    let pword = req.query.pword
    let sql = 'select * from administrators'
    let judge = false
    let accountInformation = {}
    let token = null
    db.query(sql, (err, results) => {
        if (err) console.log(err);

        results.forEach((item, index) => {
            if (item.account == account) {
                if (item.pword == pword) {
                    judge = !judge
                    accountInformation = item
                    // 登录成功 生成对应Token
                    token = Token.encrypt({account,pword})
                }
            }
        });

        res.send([token,judge, accountInformation])
    })
})

// 注册接口
router.get('/reguser', (req, res) => {
    let sql = 'insert into adminisrtators (uname,pweod,account) vlaues (?,?,?)'

    db.query(sql, (err, results) => {
        if (err) console.log('添加失败！！' + err)
        else res.send('添加成功！！！' + results)

    })
})

//测试post请求接口
router.post('/pqq', (req, res) => {
    res.send('post请求  请求体：', req.body)
})

// 修改密码接口
router.get('/updataPword', (req, res) => {

    let account = req.query.account
    let pword = req.query.pword
    console.log(account,pword)

    //修改密碼
    let updSql = `update administrators set pword = '${pword}' where account = '${account}'`
    //  turn为修改成功
    let judge = false

    db.query(updSql, (err, results) => {
        if (err) console.log('修改出错' + err.message)
        else res.send(!judge)
    })

})

// 查询单个管理员信息
router.get('/admin', (req, res) => {
    let account = req.query.account
    let sql = `select * from administrators where account = ${account}`
    db.query(sql, (err, results) => {
        if (err) console.log('没有找到' + uname, err)
        else res.send(results)
    })
})

// 修改管理员信息接口
router.get('/updata', (req, res) => {
    let sql = 'updata '
})

// 查询所有管理员信息接口
router.get('/alladmin', (req, res) => {
    let sql = 'select * from administrators'

    db.query(sql, (err, results) => {
        if (err) console.log('查询出错' + err.message)
        else res.send(results)
    })
    // res.send({
    //     code:1,
    //     info:'请求管理员'
    // })
})

// 查询所有用户信息
router.get('/alluser', (req, res) => {
    let sql = 'select * from user'

    db.query(sql, (err, results) => {
        if (err) console.log('查询出错！！！' + err.message)
        else res.send(results)
    })
})

// 查询所有商品信息接口
router.get('/allcommodity', (req, res) => {
    let sql = 'select * from commoditytable'

    db.query(sql, (err, results) => {
        if (err) console.log('查询出错' + err.message)
        else res.send(results)
    })
})

// 添加商品接口
router.get('/addcommodity', (req, res) => {
    //turn为添加成功
    let judge = false
    let obj = JSON.parse(req.query.obj)
    console.log(obj);

    let sql = `insert into commoditytable set ?`
    db.query(sql,obj,(err,results) => {
        if(err) console.log('添加出错！！！')
        else res.send(!judge)
    })

})

module.exports = router

